package com.example.wangfanwangluo.wangfanboss.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.wangfanwangluo.wangfanboss.Models.GenzongModel;
import com.example.wangfanwangluo.wangfanboss.R;

import java.util.List;

/**
 * Created by wangfanwangluo on 2017/11/28.
 */

public class GenzongAdapter extends ArrayAdapter {
    private int resourceId;

    private Context context;
    private List<GenzongModel> list;
    private GenzongModel model;

    public GenzongAdapter(Context context, int textViewResourceId, List<GenzongModel> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public GenzongModel getItem(int position) {
        return list.get(position);    }

    @Override
    public long getItemId(int position) {
        return position;    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        model = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null){

            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.ordertrack_line1 = (TextView) view.findViewById(R.id.ordertrack_line1);
            viewHolder.ordertrack_line2 = (TextView) view.findViewById(R.id.ordertrack_line2);
            viewHolder.ordertrack_line3 = (TextView) view.findViewById(R.id.ordertrack_line3);
            viewHolder.ordertrack_greenround = (TextView) view.findViewById(R.id.ordertrack_greenround);
            viewHolder.ordertrack_greyround = (TextView) view.findViewById(R.id.ordertrack_greyround);
            viewHolder.ordertrack_address = (TextView) view.findViewById(R.id.ordertrack_address);
            viewHolder.ordertrack_time = (TextView) view.findViewById(R.id.ordertrack_time);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        lineType(model.getType(), viewHolder.ordertrack_line1, viewHolder.ordertrack_line2,
                viewHolder.ordertrack_line3, viewHolder.ordertrack_greenround,
                viewHolder.ordertrack_greyround);
        viewHolder.ordertrack_address.setText(model.getAddress());
        viewHolder.ordertrack_time.setText(model.getTime());
        return view;
    }

    class ViewHolder {
        TextView ordertrack_line1;
        TextView ordertrack_line2;
        TextView ordertrack_line3;
        TextView ordertrack_greenround;
        TextView ordertrack_greyround;
        TextView ordertrack_address;
        TextView ordertrack_time;
    }

    /*
  * 列表左侧
  * @param type
  * @param views ordertrack_line1,ordertrack_line2,ordertrack_line3,ordertrack_greenround,ordertrack_greyround
  * @return void
  * */
    private void lineType(int type, View... views){
        switch (type){
            case 1://只有一条信息
                views[0].setVisibility(View.GONE);
                views[1].setVisibility(View.GONE);
                views[2].setVisibility(View.GONE);
                views[3].setVisibility(View.VISIBLE);
                views[4].setVisibility(View.GONE);
                break;
            case 2://大于一条，且当前是最新的那条
                views[0].setVisibility(View.GONE);
                views[1].setVisibility(View.VISIBLE);
                views[2].setVisibility(View.GONE);
                views[3].setVisibility(View.VISIBLE);
                views[4].setVisibility(View.GONE);
                break;
            case 3://大于一条，处于中间的那条
                views[0].setVisibility(View.VISIBLE);
                views[1].setVisibility(View.GONE);
                views[2].setVisibility(View.GONE);
                views[3].setVisibility(View.GONE);
                views[4].setVisibility(View.VISIBLE);
                break;
            case 4://大于一条，时间最久的那条
                views[0].setVisibility(View.GONE);
                views[1].setVisibility(View.GONE);
                views[2].setVisibility(View.VISIBLE);
                views[3].setVisibility(View.GONE);
                views[4].setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

}
